﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace zmMoniter
{
    class sendNeter
    {
        private string sIP = "";   
        public int isPort;


        private TcpUtilClient client;
        private IPEndPoint iep;
        public sendNeter(string sStrIP, int sStrPort)
        {        
            sIP = sStrIP;
            isPort = sStrPort;

            iep = new IPEndPoint(IPAddress.Parse(sIP), isPort);
            client = new TcpUtilClient(iep, true, 60000);
            client.loseConnected += new ClientConnectionFalseHandler(client_loseConnected);
            //client.receivMessage += new ClientReceiveMessageHander(client_receivMessage);
            Int32 result = client.Connect();
            if (result == 1)
            {
               // this.SetListBox("连接服务器" + this.iep + "成功!");

              //  this.buttonConnet.Enabled = false;
              //  this.buttonCut.Enabled = true;
            }
            else
            {
                //MessageBox.Show("连接服务器" + this.iep + "失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }

        }

        private void client_loseConnected(object sender, LoseConnectFalseEventArgs e)
        {
            //this.listBox1.Invoke(setListBoxCallback, "连接断开 原因:" + e.Reason + " 时间:" + e.FaultTime);
           // this.buttonCut.Invoke(setButton);
            //this.buttonConnet.Invoke(setButton);
        }
        private void client_receivMessage(object sender, ClientReceiveMesageEventArgs e)
        {
            string mesg = Encoding.UTF8.GetString(e.Buffer);
           // this.listBox1.Invoke(setListBoxCallback, mesg);
            MessageBox.Show(mesg, "提示", MessageBoxButtons.OK); 

        }

      

        public void seedMessage(string sMsg)
        {
            try
            {
                byte[] buffer = Encoding.UTF8.GetBytes(sMsg);
                Int32 result = client.SendMesg(buffer);
                if (result == 1)
                {
                    //MessageBox.Show("指令成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    //MessageBox.Show("指令失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch
            {
                //MessageBox.Show("请先连接服务器!");
            }
        }

        private bool IsByte(string s, out byte bit)
        {
            try
            {
                bit = byte.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
                return true;
            }
            catch
            {
                bit = default(byte);
                return false;
            }
        }
        private List<byte> calByte(string hexString)
        {
            try
            {
                string[] numbers = hexString.Trim().Split(' ');
                List<byte> buffer = new List<byte>();
                for (int i = 0; i < numbers.Length; i++)
                {
                    byte bit;
                    if (this.IsByte(numbers[i], out bit))
                    {
                        buffer.Add(bit);
                    }
                }
                return buffer;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }
    }
}
